<?php
namespace Admin\Model;
use Common\Model;
use Common\Tree;
if(!defined('SITE')) exit('Access Denied');
class TypeModel extends Model {
	public function __construct(){
		$this->tableName='arctype';
		/*
		数据是否可以不传	null
		数据类型	type	
		数值的区间	between
		数值不得在某个区间	notbetween
		数据是否属于某个范围	in
		数据不得属于某个范围	notin
		数据长度的验证	length
		数据是否可以重复	unique
		数据是否符合我们所描述的正则表达式	regex
		数据是否符合某个自定义函数	function
		数据是否符合某个方法	method
		数据是否等于某个值	equal
		数据不得等于某个值	notequal
		两条数据是否一致	confirm
		*/
		$this->validate=array(
			'id'=>array(
				'between'=>'0,4294967295',
				'unique'=>true
			),
			'pid'=>array(
				'between'=>'0,4294967295',
				'method'=>array('checkPid'),
				'method1'=>array('checkPid1',array(get('id')))
			),
			'name'=>array(
				'length'=>'1,255',
				'unique'=>true
			),
			'uname'=>array(
				'length'=>'1,255',
				'unique'=>true
			),
			'view'=>array(
				'length'=>'1,255'
			),
			'title'=>array(
				'length'=>'1,255'
			),
			'keywords'=>array(
				'length'=>'1,255'
			),
			'description'=>array(
				'length'=>'1,500'
			),
			'sort'=>array(
				'between'=>'0,4294967295'
			)
		);
		
		$this->vTime=array(
			'id'=>array(
				'type'=>array(self::INSERT,self::DELETE,self::UPDATE,self::SELECT,self::SELECT_CHECK_PK,self::UPDATE_CHECK_PK)
			),
			'pid'=>array(
				'method1'=>array(self::UPDATE)
			)
		);
		
		$this->vMessage=array(
			'pid'=>array(
				'method'=>'父栏目不存在.',
				'method1'=>'不能选择自己或者自己的后代作为父栏目.'
			)
		);
		$this->vFieldsAlias=array(
			'pid'=>'父栏目',
			'name'=>'栏目名称',
			'uname'=>'URL目录',
			'view'=>'栏目界面',
			'title'=>'SEO标题',
			'keywords'=>'SEO关键字',
			'description'=>'SEO描述',
			'sort'=>'排序'
		);
		
		parent::__construct();
	}
	public function checkPid($pid){
		if($pid>0){
			$param=array(
					'sql'=>"select `pid` from `{$this->getTableName()}` where id=?",
					'bind'=>array($this->getFieldsType('id'),array($pid))
			);
			return count($this->db->execute($param));
		}else{
			return true;
		}
	}
	public function checkPid1($pid,$id){
		$tree=new Tree($this->getFamily(array('id'=>$id)));
		if(array_key_exists($pid,$tree->getPosterity($id,true))){
			return false;
		}else{
			return true;
		}
	}
	public function getTid($id){
		$param=array(
			'sql'=>"select `tid` from {$this->getTableName()} where id=?",
			'bind'=>array($this->getFieldsType('id'),array($id))
		);
		$data=$this->db->execute($param);
		if(isset($data[0]['tid'])){
			return $data[0]['tid'];
		}else{
			return false;
		}
	}
	public function add($data,$autoValidation=true){
		if(post('pid')==0){
			$data['tid']=$this->getNextAI();
		}else{
			$data['tid']=$this->getTid(post('pid'));
		}
		return parent::add($data,$autoValidation);
	}
	public function getAll(){
		$param=array(
			'sql'=>"select * from {$this->getTableName()} order by sort"
		);
		return $this->db->execute($param);
	}
	public function update($data,$pk,$autoValidation=true){
		$family=$this->getFamily(array('id'=>$pk));
		if(post('pid')==0){
			$data['tid']=$pk;
		}else{
			$data['tid']=$this->getTid(post('pid'));
		}
		$result=parent::update($data,$pk,$autoValidation);
		if($result===false){
			return false;
		}
		if($result==0){
			return true;
		}
		//如果修改了栏目，我们应该去修改后代的tid
		if($result>0){
			$tree=new Tree($family);
			$posterityId=array_keys($tree->getPosterity($pk));
			if(count($posterityId)){
				$placeholder=implode(',',array_fill(0,count($posterityId),'?'));
				$types=$this->getFieldsType('tid').implode('',array_fill(0,count($posterityId),$this->getFieldsType('id')));
				array_unshift($posterityId,$data['tid']);
				$param=array(
					'sql'=>"UPDATE {$this->getTableName()} SET `tid`=? WHERE id in({$placeholder})",
					'bind'=>array($types,$posterityId)
				);
				$this->db->execute($param);
			}
			return true;
		}
	}
	public function delete($pk){
		$family=$this->getFamily(array('id'=>$pk));
		if(parent::delete($pk)){
			$tree=new Tree($family);
			$posterityId=array_keys($tree->getPosterity($pk));
			if(count($posterityId)){
				parent::delete($posterityId);
			}
			return true;
		}else{
			return false;
		}
	}
	use \Common\tTypeModel;
}